﻿using Asion.Dapper.SqlBuilder;
using Asion.Dapper.SqlBuilder.Condition;
using Dapper;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Data;
using System.Linq;
using System.Text;

namespace Asion.Dapper
{
    public interface IBatchUpdate : ICondition
    {

    }


    public class BatchUpdateColumn : BaseCondition, IBatchUpdate, IConditionDynamicParameters, IGetSql
    {
        private IEnumerable<object> Entitys { get; set; }

        private IEnumerable<string> UpdateColumns { get; set; }



        private string WhereColumn { get; set; }

        public BatchUpdateColumn(IEnumerable<object> entitys, string[] updateColumns, string whereColumn)
        {
            UpdateColumns = updateColumns;
            WhereColumn = whereColumn;
            Entitys = entitys;

        }



        public override string BuildSql()
        {
            var sql = new StringBuilder();


            sql.AppendFormat(" SET ");
            for (var i = 0; i < UpdateColumns.Count(); i++)
            {
                var column = UpdateColumns.ElementAt(i);
                var mapping = _custom_table_mappinghandle.GetColumnMapping(column);
                if (mapping == null)
                {
                    throw new ArgumentException($"set column that {column} does not exist ");
                }
                var whereMapping = _custom_table_mappinghandle.GetColumnMapping(WhereColumn);

                var formula = ComplexOperatorFactory.GetComplexCondtionOperatorFormula(CondtionOperator.BatchSet);
                var updateParamName = $"batch_update_{mapping.DataColumnName}";
                sql.AppendFormat(formula, _encap_sulation.EncapSulation(mapping.DataColumnName));

                sql.AppendFormat($" CASE {WhereColumn} ");
                for (int j = 0; j < Entitys.Count(); j++)
                {
                    sql.Append($"  WHEN @batch_where_{j} THEN @batch_update_value_{mapping.DataColumnName}_{j}");
                }


                sql.AppendFormat(" End ");

                sql.AppendFormat(", ");

                //参数处理
                int count = 0;
                foreach (var entity in Entitys)
                {
                    var entityType = entity.GetType();
                    var entityWhereValue = entityType.GetProperty(whereMapping.ColumnName);
                    var entityValue = entityType.GetProperty(mapping.ColumnName);
                    AddDynamicParam($"batch_where_{count}", entityWhereValue.GetValue(entity));
                    AddDynamicParam($"batch_update_value_{mapping.DataColumnName}_{count}", entityValue.GetValue(entity));
                    count++;
                }
            }
            if (sql.ToString().EndsWith(", "))
                sql.Remove(sql.Length - 2, 2);


            return sql.ToString();
        }




        public DynamicParameters GetDynamicParameters()
        {
            return DynamicParam;
        }


    }












}
